/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis;

import com.cxhd.lis.ctrl.actor.ActorManager;
import com.cxhd.lis.interceptor.GobInterceptor;
import com.cxhd.lis.model.RoleModel;
import com.cxhd.lis.model._MappingKit;
import com.cxhd.lis.shiro.ShiroInterceptor;
import com.cxhd.lis.task.TaskManager;
import com.cxhd.lis.util.CacheTool;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.ext.plugin.config.ConfigKit;
import com.jfinal.ext.plugin.config.ConfigPlugin;
import com.jfinal.ext.route.AutoBindRoutes;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.dialect.AnsiSqlDialect;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.dialect.SqlServerDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 52461
 */
public class LisConfig extends JFinalConfig {

    private ActorManager actorManager;
     private static final Log log = Log.getLog(LisConfig.class);

    @Override
    public void configConstant(Constants me) {
        PropKit.use("db.txt");
        me.setViewType(ViewType.JSP);
        me.setDevMode(false);
        // new ConfigPlugin(".*.txt").reload(false).start();
        System.out.println(PropKit.get("db.mysql.url"));
    }

    @Override
    public void configRoute(Routes me) {
        AutoBindRoutes abr = new AutoBindRoutes().autoScan(false);

        me.add(abr);
    }

    @Override
    public void configPlugin(Plugins me) {
        // 配置druid数据库连接池插件
        DruidPlugin druidPlugin_mysql = createDruidPlugin_mysql();
        me.add(druidPlugin_mysql);

        // 配置ActiveRecord插件
        ActiveRecordPlugin arp_mysql = new ActiveRecordPlugin("mysql", druidPlugin_mysql);
        arp_mysql.setDialect(new MysqlDialect());
        arp_mysql.setDevMode(false);
        arp_mysql.setShowSql(false);
        arp_mysql.addMapping("lis_system_role", "pk", RoleModel.class);

        _MappingKit.mapping(arp_mysql);
        me.add(arp_mysql);
    }

    @Override
    public void configInterceptor(Interceptors me) {
        me.add(new ShiroInterceptor());
    }

    @Override
    public void configHandler(Handlers me) {
        me.add(new ContextPathHandler("base"));
    }

    public static DruidPlugin createDruidPlugin_mysql() {
        return new DruidPlugin(PropKit.get("db.mysql.url"), PropKit.get("db.mysql.user"), PropKit.get("db.mysql.pwd"));
    }

//    public static String getConfigStr(String key) {
//
//        String str = ConfigKit.getStr(key);
//
//        LogKit.info("load config key=" + key + " value=" + str);
//
//        if (StrKit.isBlank(str)) {
//            throw new NullPointerException("config " + key + " is null please write to config file");
//        }
//
//        return str.trim();
//    }
    @Override
    public void afterJFinalStart() {
        super.afterJFinalStart(); //To change body of generated methods, choose Tools | Templates.
        try {
            CacheTool.saveAllAuth(Db.find("select * from lis_system_res"));
            List<Record> list = Db.find("select * from lis_config");
            Map<String, String> map = new HashMap<String, String>();
            if (!list.isEmpty()) {
                for (Record re : list) {
                    map.put(re.getStr("name"), re.getStr("value").replace("\\", "\\\\"));
                }
            }
            DataCache.getIntance().setConfig(map);
            DataCache.getIntance().initConfigForHisDb();
            actorManager = ActorManager.getIntance();

            actorManager.initActor();

            TaskManager.getInstance().startTask();
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("lis 系统启动完成");
        System.out.println("启动完成");
    }

    @Override
    public void beforeJFinalStop() {
        super.beforeJFinalStop(); //To change body of generated methods, choose Tools | Templates.
        actorManager.destory();
        TaskManager.getInstance().stopTask();
    }

}
